/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * menampilkan menu
 */
public class MenuCanvas extends Canvas {

    private String[] menus = {"Search", "Add New Word", "Change Orientation", "Update Database", "Exit"};
    private int jmlMenu = menus.length;
    private Image imgBg, imgSelection;
    private int x, y;
    private Font fontDefault = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    private int range = 30, selectedIdx = 0;

    public MenuCanvas() {
        try {
            imgBg = Image.createImage("/bg.png");
            imgSelection = Image.createImage("/selection.png");
        } catch (Exception e) {
        }
        x = getWidth() >> 1;
        y = getHeight() >> 1;

    }

    protected void paint(Graphics g) {
        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(imgBg, x, y, Graphics.HCENTER | Graphics.VCENTER);
        g.setFont(fontDefault);


        int yMenu = (getHeight() - (jmlMenu * (fontDefault.getBaselinePosition() + range))) >> 1;
        int xMenu = x>>1;
        for (int i = 0; i < jmlMenu; i++) {
            if (i == selectedIdx) {
                g.drawImage(imgSelection, xMenu, yMenu, Graphics.TOP | Graphics.LEFT);
                g.setColor(0xFFFFFF);
            } else {
                g.setColor(0x000000);
            }
            g.drawString(menus[i], xMenu, yMenu, Graphics.TOP | Graphics.LEFT);
            yMenu += range;
        }
    }

    private void up() {
        selectedIdx--;
        if (selectedIdx < 0) {
            selectedIdx = jmlMenu - 1;
        }
        repaint();
    }

    private void down() {
        selectedIdx++;
        if (selectedIdx >= jmlMenu) {
            selectedIdx = 0;
        }
        repaint();
    }

    private void selectMenu(){
        switch(selectedIdx){
            case 0:     //search
                KamusMidlet.display.setCurrent(KamusMidlet.midlet.frmSearch);
                break;
            case 1:     //add new word
                KamusMidlet.display.setCurrent(KamusMidlet.midlet.frmAddWord);
                break;
            case 2:     //change orientation
                KamusMidlet.display.setCurrent(KamusMidlet.midlet.listOri);
                break;
            case 3:     //update database
                KamusMidlet.display.setCurrent(KamusMidlet.midlet.frmUpdate);
                break;
            case 4:     //exit
                KamusMidlet.midlet.exitMidlet();
                break;
        }
    }

    protected void keyPressed(int keyCode) {
        switch (keyCode) {
            case -1:    //atas
                up();
                break;
            case -2:    //bawah
                down();
                break;
            case -3:    //kiri
                down();
                break;
            case -4:    //kanan
                up();
                break;
            case -5:    //tengah
                selectMenu();
                break;
            case -6:    //menu kiri
                break;
            case -7:    //menu kanan
                break;
        }
    }

    public void reset(){
        selectedIdx=0;
    }
}
